<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/gray/window.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/print.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" style="background: #F4F4F4;border-top:1px solid #95B8E7">
		</div>
		<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr style="display: none;">
						<td width="20%">
							ID
						</td>
						<td width="50%">
							<input class="easyui-textbox" id="input_ID" name="ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>出库单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>销售单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_SALESORDER_ID" name="SALESORDER_ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">*</h>用户组
						</td>
						<td width="30%">
							<input class="easyui-combobox" id="input_CUSTOMER_TYPE" name="CUSTOMER_TYPE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>用户组类型
						</td>
						<td width="30%">
							<input class="easyui-combobox" id="INPUT_PROPERTY" name="PROPERTY" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">* </h>配送单位
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_CUSTOMER_NAME" name="CUSTOMER_NAME" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>配送时间
						</td>
						<td width="30%">
							<input class="easyui-datebox" id="input_DISTRIBUTION_DATE" name="DISTRIBUTION_DATE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
				</table>

				<table id="tt">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" class="print_text_style" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" class="print_text_style" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
				</div>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/easyui-lang-zh_CN.js"></script>
	</body>
	<script type="text/javascript">
		var _dataRow;
		var currid = AddFlag; //当前页面的ID
		var id = getID();
		var saleid = id;
		var updateData; //保存拷贝的数据
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify;
		var _detailToolbarData;
		var auth = "admin;" + hex_sha1("admin1");
		_detailToolbarData = AddDetailToolBar(2);
		$(document).ready(function(){
			if(saleid != AddFlag && saleid != undefined) {
			currid = id;
			loadRemote();
		}
		});
		$('#INPUT_PROPERTY').combobox({
			panelHeight: "50px",
			valueField:'id',
    		textField:'text',
    		data:[{
    			id:'0',
    			text:'实时定价'
    		},{
    			id:'1',
    			text:'后定价'
    		}]
		})
		
		///增加脚注
		addFooter($("#new_footer"));
		//增加单据的toolbar
		var receipts_type = RECEIPT_FRONT_SYMBOL + RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL + RECEIPT_VERIFY_SYMBOL+RECEIPT_UNVERIFY_SYMBOL;
		receipts_type +=  RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		GetDetailTable();

		function print() {
			if(isVerify==0)
			{
				$.messager.alert("警告","订单未审核，不能打印");
				return;
			}
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert("错误", "当前页面为新增页面不能打印");
				return;
			} else {
				pLocation = "SalesSettlementPrint.html?id=" + currid;
				window.open(pLocation);
			}
		}

		function frontpage() {
			if(currPage >= MinPage) {
				var _url = RemoteLinkAddress + '/logistics/receiveorder/pre.do?ID=' + currid;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() {
			if(currPage <= MaxPage) {
				var _url = RemoteLinkAddress + '/logistics/receiveorder/next.do?ID=' + currid;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function verifydata() {
//			if(!isTrueData()) {
//				$.messager.alert("警告", "请确认实际单价与实收数量是否填写完整");
//				return;
//			}
			if(isVerify==1)
			{
				$.messager.alert("警告","订单已经审核，无须再审");
				return;
			}
			$.messager.confirm("确认", "是否审核", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/receiveorder/verify.do", {
						"ID": currid,
					}, function(result) {
						if(result.msg == "success") {
							$.messager.alert({
								title: '成功',
								msg: '审核成功!'
							});
							isVerify=1;
							verifyStatus(isVerify, true);
						} else {
							$.messager.alert({
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}
		
		function unverifydata() {
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/receiveorder/unverify.do", {
							"ID": currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show error message
									title: '成功',
									msg: '放弃审核成功!'
								});
								isVerify = 0;
								verifyStatus(isVerify, true);
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						updateData = deepCopy(result);
						currPage = result.ID; //表示传回的ID
						currid = result.ID;
						_dataRow = result; //将获取到的数据中字头为rows的数据赋值给_data
						var _data = _dataRow.rows;
						_data = changeRealData(_data);
						for(var singleData of _data){
							singleData.REAL_SUM = singleData.REAL_SUM.toFixed(2);
						}
						$('#tt').edatagrid('loadData', _data); //调用easyui的loadData方法，将数据写入id为tt的datagrid中
						getFormData(result);
						CalSum();
						isVerify = result.ISVERIFY;
						verifyStatus(result.ISVERIFY, true);
					}

				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function searchmore() {
			AddTabIn("销售结算", parent.$('#tabs'), "UI/LOGISTICS/SalesSettlementOrder/SalesSettlementSearch.html");
		}

		function save() {
			if(isVerify==1)
			{
				$.messager.alert("警告","订单已审核，不能保存");
				return;
			}
			getData();
		}

		getPageMaxAndMin();

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/logistics/receiveorder/getmaxminid.do";
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/logistics/receiveorder/findbyid.do?ID=' + saleid;
			getRemoteData(_url);
		}

		$('#input_sum').textbox({
			onChange: function(value) {
				var _trim = $.trim(value);
				var s = changeMoneyToChinese(_trim);
				$('#span_sum').html(s);
			}
		});

		function GetDetailTable() { //构造datagrid中的数据
			$('#tt').edatagrid({
				height: 310,
				title: '',
				toolbar: _detailToolbarData, //toolbar选择自定义的名为toolbar的工具栏
				collapsible: true, //是否折叠：是
				singleSelect: true, //是否单选：是
				fitColumns: true, //自动调整列：是
				autoSave: true,
				columns: [
					[{
						field: 'SALESORDERDETAIL_ID',
						title: 'ID',
						width: '0%',
						align: 'center',

					}, {
						field: 'INTERNAL_NAME',
						title: '内部商品名',
						width: '11%',
						align: 'center',

					}, {
						field: 'UNIT_NAME',
						title: '原始单位',
						width: '5%',
						align: 'center',

					}, {
						field: 'INOUT_UNIT',
						title:'*单位',
						width:'5%',
						align:'center',
						editor: {
							type: 'textbox'
						},
						formatter: function(value, row, index) {
							if(value === '') {
								row.INOUT_UNIT = row.UNIT_NAME;
								return row.UNIT_NAME;
							}else{
								return value;
							}
						}
					},{
						field: 'UNIT_PRICE',
						title: '计划单价',
						width: '10%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}, {
						field: 'REAL_PRICE',
						title: '实际单价',
						width: '10%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								precision: 2, //保留两位
								groupSeparator: ',', //每组用','隔开
								decimalSeparator: '.',
								prefix: '￥' //在数据前面加￥符号
							}

						},
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}, {
						field: 'AMOUNT',
						title: '数量',
						width: '11%',
						align: 'center',

					}, {
						field: 'REAL_AMOUNT',
						title: '实收数量',
						width: '11%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
								precision: 2,
								groupSeparator: ',',
								decimalSeparator: '.',

							}
						},

					}, {
						field: 'REAL_SUM',
						title: '总价',
						width: '11%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								var m=parseFloat(value);
								m=m.toFixed(2);
								return "￥" + m;
							}
						}
					}, {
						field: 'PROCESSING_MODE',
						title: '退货处理',
						width: '11%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: 'text',
								textField: 'text',
								data: [{
									'text': '收货'
								}, {
									'text': '退货未退回'
								}, {
									'text': '退货入库'
								}]
							}
						}
					},{
						field: 'RETURN_AMOUNT',
						title: '退货数量',
						width: '11%',
						align: 'center',
						formatter: function(value, row, index) {
							return value;
						},
						editor: {
							type: 'numberbox',
						}
					}]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					console.log(updateData.PROPERTY);
					var amount = row.REAL_AMOUNT; //将该行name为AMOUNT的数据赋值给amount
					var perprice = row.REAL_PRICE; //同上
					var result = amount * perprice; //将上述两个的值相乘再复制给result
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							REAL_SUM: result.toFixed(2)
						}
					});
					var realAmount=parseFloat(row.REAL_AMOUNT);//实收数量
					var returnAmount=parseFloat(row.RETURN_AMOUNT);//退货数量
					if(returnAmount > realAmount) {
						$.messager.alert('警示', "退货数量不能超过实收数量！");
						row.RETURN_AMOUNT = "";
					}
					$('#tt').edatagrid('refreshRow', index);
					CalSum();

				}
			});
		}
		$('#tt').datagrid('hideColumn', 'SALESORDERDETAIL_ID');

		function submitData(_data) {
			var _senddata = JSON.stringify(_data);
			console.log(_data);
			$.ajax({
				dataType: 'JSON',
				url: RemoteLinkAddress + "/logistics/receiveorder/update.do",
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					$.messager.alert("提示信息", "保存成功");
				},
				error: function(result) {
					$.messager.alert("错误信息", "保存失败");
				}
			});
		}

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				_num += parseFloat(dataGridRows[i].REAL_SUM);
			}
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
			$("input[name='number']").val(_num.toFixed(2));
		}
		
		//将预计值赋给实际值
		function changeRealData(data){
			for(var s in data){
				if(data[s].REAL_PRICE == 0 || data[s].REAL_PRICE == undefined || data[s].REAL_PRICE === ""){
					data[s].REAL_PRICE = data[s].UNIT_PRICE;
				}if(data[s].REAL_AMOUNT == 0 || data[s].REAL_AMOUNT == undefined || data[s].REAL_AMOUNT === ""){
					data[s].REAL_AMOUNT = data[s].AMOUNT;
				}
				data[s].REAL_SUM = data[s].REAL_PRICE * data[s].REAL_AMOUNT;
			}
			return data;
		}

		function isTrueData() {
			var row = $("#tt").datagrid("getRows");
			var _this;
			for(var i = 0, len = row.length; i < len; i++) {
				_this = row[i];
				for(var s in _this) {
					if(s === "REAL_PRICE" || s === "REAL_AMOUNT") {
						if(_this[s] == "") {
							return false;
						}
					}
				}
			}
			return true;
		}

		function getData() {
			var _dataPage = submitFormData();
			_dataPage.rows = _dataRow.rows;
			_dataPage.FINALSUM=$("input[name='number']").val();
			submitData(_dataPage);
		}
		
		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						dataType: "json", //服务器返回的数据类型
						url: RemoteLinkAddress + "/logistics/receiveorder/excelorder.do",
						data: {
							"ID": currid
						},
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}
	</script>

</html>